<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit\Ctl;

use Srv\Apps\Proto\DTO\Skit\SkitLabelAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitLabelEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitLabelListDTO;
use Srv\Apps\Proto\VO\Skit\SkitLabelListVO;
use Srv\Apps\Proto\VO\Skit\SkitLabelVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class SkitLabelCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $sklbId
     * @return array
     * getSkitLabelSrc
     */
    public function getSkitLabelSrc(int $sklbId):array
    {
        if($sklbId < 1) return [];
        $isFail         = false;
        $labelRecord    = $this->DbAuto->getDataOne('SELECT sklb_id, sklb_name, sklb_show, sklb_create_time, sklb_update_time FROM '.$this->DbAuto->getTableNameAll('skit_label').' WHERE sklb_id='.$sklbId.' AND sklb_delete=0', $isFail);
        if($isFail || !isset($labelRecord['sklb_id'])) return [];
        return $labelRecord;
    }

    /**
     * @param array $sklbIdList
     * @return array
     * getSkitLabelSrcBatch
     */
    public function getSkitLabelSrcBatch(array $sklbIdList):array
    {
        if(count($sklbIdList) < 1) return [];
        $isFail             = false;
        $labelRecordList    = $this->DbAuto->getData('SELECT sklb_id, sklb_name, sklb_show, sklb_create_time, sklb_update_time FROM '.$this->DbAuto->getTableNameAll('skit_label').' WHERE sklb_id IN('.implode(',', $sklbIdList).') AND sklb_delete=0', $isFail);
        if($isFail) return [];
        return $labelRecordList;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return SkitLabelListVO|null
     * getSkitLabelList
     */
    public function getSkitLabelList(Page $Page, array $whereData): ?SkitLabelListVO
    {
        $limitString            = $Page->getLimitString();
        $whereString            = ' WHERE sklb_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case SkitLabelListDTO::_sklbId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'sklb_id='. intval($val);
                    break;
                case SkitLabelListDTO::_name:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'sklb_name LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitLabelListDTO::_show:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'sklb_show='. intval($val);
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS sklb_id, sklb_name, sklb_show, sklb_create_time, sklb_update_time FROM '.$this->DbAuto->getTableNameAll('skit_label').$whereString.' ORDER BY sklb_id DESC '.$limitString;
        $labelRecordList        = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $SkitLabelVOList        = [];
        if(CommArray::isForeach($labelRecordList))foreach($labelRecordList as $labelRecord){
            $sklbIdItem         = intval($labelRecord['sklb_id']);
            if($sklbIdItem < 1) continue;
            $SkitLabelVO        = $this->getSkitLabelVO($labelRecord);
            if(!($SkitLabelVO instanceof SkitLabelVO)) continue;
            $SkitLabelVOList[]  = $SkitLabelVO;
        }
        $SkitLabelListVO        = new SkitLabelListVO();
        $SkitLabelListVO->setList($SkitLabelVOList);
        $SkitLabelListVO->setPage($Page->getPageVO($totalNum));
        return $SkitLabelListVO;
    }

    /**
     * @param int $sklbId
     * @return SkitLabelVO|null
     * getSkitLabelInfo
     */
    public function getSkitLabelInfo(int $sklbId):?SkitLabelVO
    {
        $labelSrc   = $this->getSkitLabelSrc($sklbId);
        if(!isset($labelSrc['sklb_id'])) return null;
        return $this->getSkitLabelVO($labelSrc);
    }

    /**
     * @param array $sklbIdList
     * @return array|null
     * 批量获取标签
     */
    public function getSkitLabelInfoBatch(array $sklbIdList):?array
    {
        $SkitLabelVOList    = [];
        $labelList          = $this->getSkitLabelSrcBatch($sklbIdList);
        if(count($labelList) < 1) return $SkitLabelVOList;
        foreach ($labelList as $labelRecord){
            $sklbIdItem     = intval($labelRecord['sklb_id']??0);
            if($sklbIdItem < 1) continue;
            $SkitLabelVO    = $this->getSkitLabelVO($labelRecord);
            if($SkitLabelVO instanceof SkitLabelVO) $SkitLabelVOList[$sklbIdItem]   = $SkitLabelVO;
        }
        return $SkitLabelVOList;
    }

    /**
     * @param array $labelRecord
     * @return SkitLabelVO|null
     * getSkitLabelVO
     */
    public function getSkitLabelVO(array $labelRecord):?SkitLabelVO
    {
        $sklbId             = intval($labelRecord['sklb_id']??0);
        if($sklbId < 1) return null;
        $SkitLabelVO        = new SkitLabelVO();
        $SkitLabelVO->setSklbId($sklbId);
        $SkitLabelVO->setName(trim($labelRecord['sklb_name']));
        $SkitLabelVO->setShow(intval($labelRecord['sklb_show']));
        $SkitLabelVO->setCreateTime(intval($labelRecord['sklb_create_time']));
        $SkitLabelVO->setUpdateTime(intval($labelRecord['sklb_update_time']));
        return $SkitLabelVO;
    }

    /**
     * @param array $setData
     * @return int
     * 新增短剧标签
     */
    public function skitLabelAdd(array $setData):int
    {
        if(count($setData) < 1) return 0;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitLabelAddDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sklb_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitLabelAddDTO::_show:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sklb_show='.intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $currTime   = CommTime::getTimeStamp(0);
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('skit_label').' SET '.$setString.', sklb_delete=0, sklb_create_time='.$currTime.', sklb_update_time='.$currTime;
        $sklbId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $sklbId < 1) return 0;
        return $sklbId;
    }

    /**
     * @param int $sklbId
     * @param array $setData
     * @return bool
     * 编辑短剧标签
     */
    public function skitLabelEdit(int $sklbId, array $setData):bool
    {
        if($sklbId < 1 || count($setData) < 1) return false;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitLabelEditDTO::_name:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'sklb_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitLabelEditDTO::_show:
                    $setString  .= (strlen($setString) > 0 ? ', ' : '') . 'sklb_show='.intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp(0);
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('skit_label').' SET '.$setString.', sklb_update_time='.$currTime.' WHERE sklb_id='.$sklbId.' AND sklb_delete=0';
        if($this->DbAuto->getDataNum($sql, $isFail) < 1 || $isFail) return false;
        return true;
    }
}